package router

import (
	"net/http"

	"10apiserver/controllers/article"
	"10apiserver/controllers/computerinfo"
	"10apiserver/router/middleware"

	"github.com/gin-gonic/gin"
)

// Load loads the middlewares, routes, handlers.
func Load(g *gin.Engine, mw ...gin.HandlerFunc) *gin.Engine {
	// Middlewares.
	g.Use(gin.Recovery())
	g.Use(middleware.NoCache)
	g.Use(middleware.Options)
	g.Use(middleware.Secure)
	g.Use(mw...)
	// 404 Handler.
	g.NoRoute(func(c *gin.Context) {
		c.String(http.StatusNotFound, "The incorrect API route.")
	})

	// The health check handlers
	ci := g.Group("/ci")
	{
		ci.GET("/health", computerinfo.HealthCheck)
		ci.GET("/disk", computerinfo.DiskCheck)
		ci.GET("/cpu", computerinfo.CPUCheck)
		ci.GET("/ram", computerinfo.RAMCheck)
	}

	// The health check handlers
	art := g.Group("/article")
	{
		art.POST("", article.Create)
		art.PUT("", article.Update)
		art.PUT("/inc/:path", article.IncReadCount)
		art.DELETE("/:id", article.Delete)
	}

	arts := g.Group("/articles")
	{
		arts.GET("", article.List)
		arts.GET("/id/:id", article.GetByID)
		arts.GET("/path/:path", article.GetByPath)
		arts.DELETE("/path/:path", article.DeleteByPath)
	}
	return g
}
